package com.robert.vesta.rest;

import com.robert.vesta.service.intf.IdService;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * kelen  2019/4/13
 * desc: 测试
 */


@RestController
public class TestController {

    private final IdService idService;


    private int count ;

    /**
     * 线程 safe
     */
    private AtomicInteger atomicInteger;


    public TestController() {
        count = 0;
        atomicInteger = new AtomicInteger(0);

        System.err.println("TestController  load");

        ApplicationContext ac = new ClassPathXmlApplicationContext(
                "spring/vesta-rest-main.xml");

        idService = (IdService) ac.getBean("idService");
    }

    @RequestMapping("/test/genId")
    public long genId() {

        return idService.genId();
    }


    @RequestMapping(value = "/test")
    public int getLocation(){

        return idService.hashCode();
    }



    @RequestMapping("/test/count")
    public AtomicInteger aotoCount() {
        atomicInteger.incrementAndGet();

        return atomicInteger;
    }


    Lock lock = new ReentrantLock();

    @RequestMapping("/test/auto")
    public int count() {

        try {
            lock.lock();
            count++;
        } catch (Exception e){

        } finally {
            lock.unlock();
        }

        return count;
    }

    //不加锁

    @RequestMapping("/test/intAdd")
    public int countInt() {

        count++;

        return count;
    }






}
